<template>
    <div class="category">
      <home-header></home-header>
      <home-nav :navIndex="navIndex"></home-nav>
      <div class="content" >
        <div class="type-search">
          <div class="top">
            <div></div>
          </div>
          <div class="cen">
            <span class="sug">全部年份</span>
            <div class="time">
              <span class="t-item" v-for="(titem,tindex) in timeList" :key="tindex" @click="dateChange(titem)">{{titem.txt}}</span>
            </div>
          </div>
          <div class="bot">
            <div class="source">
              <span class="s-item" v-for="(sitem,sindex) in sourceList" :key="sindex" @click="checkSource(sitem)">{{sitem.txt}}</span>
            </div>
          </div>
        </div>
        <div class="list">
          <div class="item" v-for="(item,index) in list" :key="index" @click="checkItem(item)">
            <list-item :item="item"></list-item>
          </div>
        </div>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="currentPage"
          :page-size="pagelimit"
          layout="total, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
      <bottom></bottom>
    </div>
</template>

<script>
import HomeHeader from '@/components/HomeHeader'
import HomeNav from '@/components/HomeNav'
import Bottom from '../components/Bottom'
import ListItem from '@/components/ListItem'
import {getMovieByPid, getMovieByYear, getMovieBySourceId, searchMovieList} from '@/api/movieList'
export default {
  name: 'Category',
  components: {
    HomeHeader,
    HomeNav,
    Bottom,
    ListItem
  },
  data () {
    return {
      navIndex: 0,
      cate: {
        title: '全部结果',
        source: '电视台'
      },
      timeList: [],
      sourceList: [],
      list: [],
      total: 0, // 总数据条数
      currentPage: 1, // 当前页
      pagelimit: 18, // 页面展示数据条数
      pid: '',
      keyword: '',
      source_id: 0,
      year: 0,
      loading: true
    }
  },
  created () {
    this.navIndex = this.$route.query.index
    if (this.navIndex > 0) {
      this.fetchData()
    }
    this.keyword = this.$route.query.keyword
    if (this.keyword) {
      this.searchData()
    }
  },
  methods: {
    searchData () {
      let params = {
        title: this.keyword,
        limit: this.pagelimit,
        page: this.currentPage - 1
      }
      searchMovieList(params).then(response => {
        let data = response.data
        this.list = data.list
        this.total = data.total
        this.loading = false
      })
    },
    fetchData () {
      this.timeList = []
      let params = {
        pid: this.$route.query.id,
        limit: this.pagelimit,
        page: this.currentPage - 1
      }
      getMovieByPid(params).then(response => {
        let data = response.data
        this.list = data.list.list
        this.total = data.list.total
        for (let i = 0; i < data.year.length; i++) {
          this.timeList.push({'txt': data.year[i].show_date, 'time': data.year[i].show_date})
        }
        this.sourceList = data.category
        this.loading = false
      })
    },
    dateChange (item) {
      if (this.year != item.time) {
        this.currentPage = 1
      }
      this.year = item.time
      this.source_id = 0
      let params = {
        pid: this.$route.query.id,
        year: item.time,
        limit: this.pagelimit,
        page: this.currentPage - 1
      }
      getMovieByYear(params).then(response => {
        let data = response.data
        this.list = data.list.list
        this.total = data.list.total
      })
    },
    checkSource (item) {
      if (this.source_id != item.source_id) {
        this.currentPage = 1
      }
      this.source_id = item.source_id
      this.year = 0
      let params = {
        source_id: this.source_id,
        limit: this.pagelimit,
        page: this.currentPage - 1
      }
      getMovieBySourceId(params).then(response => {
        let data = response.data
        this.list = data.list.list
        this.total = data.list.total
      })
    },
    checkItem (item) {
      let routeUrl = this.$router.resolve({
        path: '/detail',
        query: {id: item.source_id, index: this.$route.query.index}
      })
      window.open(routeUrl.href, '_blank')
      // this.$router.push({path: '/detail',query:{id:item.source_id,index:this.$route.query.index}})
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      this.currentPage = val
      if (this.source_id > 0 && this.year == 0) {
        this.checkSource({'source_id': this.source_id})
      } else if (this.year > 0 && this.source_id == 0) {
        this.dateChange({'time': this.year})
      } else if (this.source_id == 0 && this.year == 0 && !this.keyword) {
        this.fetchData()
      } else if (this.keyword) {
        this.searchData()
      }
    },
    watchrouter () { // 如果路由有变化，执行的对应的动作
      this.navIndex = this.$route.query.index
      this.keyword = this.$route.query.keyword
      this.currentPage = 1
      if (this.navIndex > 0) {
        this.fetchData()
      } else {
        this.searchData()
      }
    }
  },
  watch: {
    $route: 'watchrouter'
  }
}
</script>

<style lang="scss" scoped>
.category{
  .content{
    width: 980px;
    margin: auto;
    min-height: 500px;
    .type-search{
      margin-top: 20px;
      .top{
        color: #333;
        font-size: 20px;
        border-bottom: 2px solid #e5e5e5;
        display: flex;
        justify-content: space-between;
        align-items: center;
        .title{
          margin-bottom: -2px;
          padding: 5px;
          border-bottom: 2px solid #00C064;
        }
      }
      .cen{
        padding: 5px;
        font-size: 14px;
        border-bottom: 1px dashed #e5e5e5;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        .sug{
          min-width: 65px;
          line-height: 30px;
          color: #fff;
          text-align: center;
          background: #00C064;
          cursor: pointer;
        }
        .time{
          display: flex;
          flex-wrap: wrap;
          align-items: center;
          .t-item{
            margin: 5px 10px;
            padding: 0 5px;
            line-height: 30px;
              &:hover{
              color: #fff;
              background: #00C064;
              cursor: pointer;
            }
          }
        }
      }
      .bot{
        padding: 5px 0;
        display: flex;
        .sug{
          min-width: 50px;
          line-height: 30px;
          color: #000;
          text-align: center;
          font-weight: bold;
        }
        .source{
          display: flex;
          flex-wrap: wrap;
          align-items: center;
          .s-item{
            margin: 0 5px;
            padding: 0 5px;
            line-height: 30px;
            color: #00C064;
            cursor: pointer;
            &:hover{
              text-decoration: underline;
            }
          }
        }
      }
    }
    .list{
      display: flex;
      flex-wrap: wrap;
      .item{
        margin-right: 20px;
        margin-bottom: 20px;
        width: 143px;
        height: 220px;
      }
    }
    .el-pagination{
      margin: 20px 0;
      &.is-background{
        >>>.el-pager{
          .active{
            background: #00C064;
          }
          li:hover{
            color: #fff;
            background: #00C064;
          }
        }
      }
    }
  }
  .el-pagination{
    text-align: center;
  }
}
</style>
